home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvitops / util.c < prev    next >
C/C++ Source or Header  |  1991-01-25  |  8KB  |  413 lines

  1. static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/util.c,v 1.7 90/10/16 07:56:29 jjc Exp $";
  2.  
  3. #include "util.h"
  4.  
  5. enum message_type history = INFORMATION;
  6.  
  7. #ifdef STDARG
  8. void message(enum message_type type, char *format,...)
  9. #else
  10. void message(va_alist)
  11. va_dcl
  12. #endif
  13. {
  14.     int c, n;
  15.     char *s;
  16. #ifndef STDARG
  17.     enum message_type type;
  18.     char *format;
  19. #endif
  20.     va_list ap;
  21. #ifdef STDARG
  22.     va_start(ap, format);
  23. #else
  24.     va_start(ap);
  25.     type = va_arg(ap, enum message_type);
  26.     format = va_arg(ap, char *);
  27. #endif
  28.     if (program_name != NULL) {
  29.         fputs(program_name, stderr);
  30.         fputs(": ", stderr);
  31.     }
  32.     switch (type) {
  33.     case FATAL_ERROR:
  34.         fputs("fatal error: ", stderr);
  35.         break;
  36.     case ERROR:
  37.         fputs("error: ", stderr);
  38.         break;
  39.     case WARNING:
  40.         fputs("warning: ", stderr);
  41.         break;
  42.     }
  43.     while ((c = *format++) != '\0')
  44.         if (c == '%') {
  45.             c = *format++;
  46.             switch(c) {
  47.             case '%':
  48.                 putc('%', stderr);
  49.                 break;
  50.             case 'd':
  51.                 n = va_arg(ap, int);
  52.                 fprintf(stderr, "%d", n);
  53.                 break;
  54.             case 's':
  55.                 s = va_arg(ap, char *);
  56.                 fputs(s, stderr);
  57.                 break;
  58.             default:
  59.                 fprintf(stderr, "...whoops! error while handling error\n");
  60.                 exit(FATAL_ERROR);
  61.             }
  62.         }
  63.         else
  64.             putc(c, stderr);
  65.     fputc('\n', stderr);
  66.     va_end(ap);
  67.     if (type == FATAL_ERROR)
  68.         exit(FATAL_ERROR);
  69.     if ((int)type > (int)history)
  70.         history = type;
  71. }
  72.  
  73. static int safecat(s1, s2, n)
  74. char *s1, *s2;
  75. int n;
  76. {
  77.     if (strlen(s1) + strlen(s2) + 1 > n)
  78.         return FALSE;
  79.     strcat(s1, s2);
  80.     return TRUE;
  81. }
  82.  
  83. /* does s1 end with s2 ? */
  84. static int strends(s1, s2)
  85. char *s1, *s2;
  86. {
  87.     int n2 = strlen(s2);
  88.     int n1 = strlen(s1);
  89.     if (n1 < n2)
  90.         return FALSE;
  91. #ifdef CASE_INSENSITIVE_FILENAMES
  92.     return stricmp(s1 + n1 - n2, s2) == 0;
  93. #else
  94.     return strcmp(s1 + n1 - n2, s2) == 0;
  95. #endif
  96. }
  97.  
  98. /* if ext is not NULL and filename does not end with ext,
  99.    ext will be appended to filename */
  100.  
  101. FILE *xfopen(filename, is_binary, al, ext)
  102. char *filename, *al, *ext;
  103. int is_binary;
  104. {
  105.     char path[FILENAME_MAX+1];
  106.     static char list_sep[2] = {AREA_LIST_SEP, '\0'};
  107. #ifdef AREA_SEP
  108.     static char sep[2] = {AREA_SEP , '\0'};
  109. #endif /* AREA_SEP */
  110.     char *area;
  111.     char area_list[FILENAME_MAX*4];
  112.     if (al == NULL || FILENAME_HAS_AREA(filename)) {
  113.         if (ext != NULL && !strends(filename, ext)) {
  114.             path[0] = '\0';
  115.             safecat(path, filename, sizeof(path));
  116.             safecat(path, ext, sizeof(path));
  117.             if (is_binary)
  118.                 return FOPEN_RB(path);
  119.             else
  120.                 return fopen(path, "r");
  121.         }
  122.         else {
  123.             if (is_binary)
  124.                 return FOPEN_RB(filename);
  125.             else
  126.                 return fopen(filename, "r");
  127.         }
  128.     }
  129.     if (strlen(al)+1 > sizeof(area_list))
  130.         message(FATAL_ERROR, "directory list too long: %s", al);
  131.     strcpy(area_list, al);
  132.     for (area = strtok(area_list, list_sep);
  133.          area != NULL; 
  134.          area = strtok(NULL, list_sep)) {
  135. #ifdef AREA_SEP
  136.         char *p;
  137. #endif /* AREA_SEP */
  138.         FILE *fp;
  139.         path[0] = '\0';
  140. #ifndef POSTFIX_AREA
  141.         if (!safecat(path, area, sizeof(path)))
  142.             continue;
  143. #ifdef AREA_SEP
  144.         p = path + strlen(path);
  145.         if (p > path && p[-1] != AREA_SEP
  146. #ifdef AREA_SEP2
  147.             && p[-1] != AREA_SEP2
  148. #endif /* AREA_SEP2 */
  149.             ) {
  150.             if (!safecat(path, sep, sizeof(path)))
  151.                 continue;
  152.         }
  153. #endif /* AREA_SEP */
  154. #endif /* not POSTFIX_AREA */
  155.         if (!safecat(path, filename, sizeof(path)))
  156.             continue;
  157.         if (ext != NULL && !strends(filename, ext))
  158.             safecat(path, ext, sizeof(path));
  159. #ifdef POSTFIX_AREA
  160. #ifdef AREA_SEP
  161.         if (area[0] != AREA_SEP
  162. #ifdef AREA_SEP2
  163.             && area[0] != AREA_SEP2
  164. #endif /* AREA_SEP2 */
  165.             ) {
  166.             if (!safecat(path, sep, sizeof(path)))
  167.                 continue;
  168.         }
  169. #endif /* AREA_SEP */
  170.         if (!safecat(path, area, sizeof(path)))
  171.             continue;
  172. #endif /* POSTFIX_AREA */
  173.         if (is_binary) {
  174.             if ((fp = FOPEN_RB(path)) != NULL)
  175.                 return fp;
  176.         }
  177.         else {
  178.             if ((fp = fopen(path, "r")) != NULL)
  179.                 return fp;
  180.         }
  181.     }
  182.     return NULL;
  183. }
  184.  
  185. #ifdef CASE_INSENSITIVE_FILENAMES
  186.  
  187. int stricmp(s1, s2)
  188. char *s1, *s2;
  189. {
  190.     for (;;) {
  191.         char c1 = *s1, c2 = *s2;
  192.         if (c1 == '\0') {
  193.             if (c2 == '\0')
  194.                 return 0;
  195.             else
  196.                 return -1;
  197.         }
  198.         else if (c2 == '\0')
  199.             return 1;
  200.         if (isascii(c1) && isupper(c1))
  201.             c1 = tolower(c1);
  202.         if (isascii(c2) && isupper(c2))
  203.             c2 = tolower(c2);
  204.         if (c1 != c2)
  205.             return c1 - c2;
  206.         s1++;
  207.         s2++;
  208.     }
  209. }
  210.  
  211. #endif /* CASE_INSENSITIVE_FILENAMES */
  212.  
  213. #ifdef NEED_MEM_FUNCTIONS
  214.  
  215. int memcmp(s1, s2, n)
  216. char *s1, *s2;
  217. int n;
  218. {
  219.   for (; --n >= 0; s1++, s2++)
  220.     if (*s1 != *s2)
  221.       return *s1 - *s2;
  222.   return 0;
  223. }
  224.  
  225. char *memcpy(s1, s2, n)
  226. char *s1, *s2;
  227. int n;
  228. {
  229.     char *p = s1;
  230.     while (--n >= 0)
  231.         *p++ = *s2++;
  232.     return s1;
  233. }
  234.  
  235. char *memset(s, c, n)
  236. char *s;
  237. int c, n;
  238. {
  239.     char *p = s;
  240.     while (--n >= 0)
  241.         *p++ = c;
  242.     return s;
  243. }
  244.  
  245. #endif /* NEED_MEM_FUNCTIONS */
  246.  
  247. #ifdef NEED_QSORT
  248.  
  249. /* This is a heap sort. */
  250.  
  251. void qsort(v, n, width, compar)
  252. char *v;
  253. int n, width;
  254. int (*compar)();
  255. {
  256.     char *ip;
  257.     char *buf;
  258.     char *last = v + (n - 1)*width;
  259.     if ((buf = malloc(width)) == 0)
  260.         out_of_memory();
  261.     for (ip = v + (n/2)*width; ip >= v; ip -= width) {
  262.         char *jp = ip;
  263.         memcpy(buf, ip, width);
  264.         for (;;) {
  265.             char *cp = jp + (jp - v) + width;
  266.             if (cp < last) {
  267.                 char *ocp = cp + width;
  268.                 if ((*compar)(ocp, cp) > 0)
  269.                     cp = ocp;
  270.             }
  271.             else if (cp != last)
  272.                 break;
  273.             if ((*compar)(buf, cp) >= 0)
  274.                 break;
  275.             memcpy(jp, cp, width);
  276.             jp = cp;
  277.         }
  278.         memcpy(jp, buf, width);
  279.     }
  280.     ip = last;
  281.     while (ip > v) {
  282.         char *jp = v;
  283.         int j;
  284.         memcpy(buf, ip, width);
  285.         memcpy(ip, v, width);
  286.         ip -= width;
  287.         for (;;) {
  288.             char *cp = jp + (jp - v) + width;
  289.             if (cp < ip) {
  290.                 char *ocp = cp + width;
  291.                 if ((*compar)(ocp, cp) > 0)
  292.                     cp = ocp;
  293.             }
  294.             else if (cp != ip)
  295.                 break;
  296.             memcpy(jp, cp, width);
  297.             jp = cp;
  298.         }
  299.         j = (jp - v)/width;
  300.         while (j != 0) {
  301.             char *kp;
  302.             j = (j - 1)/2;
  303.             kp = v + j*width;
  304.             if ((*compar)(kp, buf) >= 0)
  305.                 break;
  306.             memcpy(jp, kp, width);
  307.             jp = kp;
  308.         }
  309.         memcpy(jp, buf, width);
  310.     }
  311.     free(buf);
  312. }
  313.  
  314. #endif /* NEED_QSORT */
  315.  
  316. #ifdef NEED_STRTOK
  317.  
  318. char *strtok(s1, s2)
  319. char *s1, *s2;
  320. {
  321.     static char *next;
  322.     register char *s = s1 != NULL ? s1 : next;
  323.     char *start;
  324.     for (;;) {
  325.         register char *p;
  326.         register char c = *s;
  327.         if (c == '\0') {
  328.             next = s;
  329.             return NULL;
  330.         }
  331.         for (p = s2; *p != '\0' && *p != c; p++)
  332.             ;
  333.         if (*p == '\0')
  334.             break;
  335.         s++;
  336.     }
  337.     start = s;
  338.     for (;;) {
  339.         register char *p;
  340.         register char c = *++s;
  341.         if (c == '\0') {
  342.             next = s;
  343.             return start;
  344.         }
  345.         for (p = s2; *p != '\0'; p++)
  346.             if (*p == c) {
  347.                 *s = '\0';
  348.                 next = s + 1;
  349.                 return start;
  350.             }
  351.     }
  352. }
  353.  
  354. #endif /* NEED_STRTOK */
  355.  
  356. #ifdef PRIMOS_GETENV
  357.  
  358. /* Prime C doesn't provide a getenv function, but there are 'global
  359. variables' that are like enviroment variables. The routine gvget will
  360. retrieve the value of one of these, so we can write a getenv using it.
  361. */
  362.  
  363.  
  364. char *getenv(name)
  365. char *name;
  366. {
  367.     char temp[32];
  368.     extern char *strncat(), *gvget();
  369.     
  370.     strcpy(temp,".");
  371.     return(gvget(strncat(temp,name,32)));
  372. }
  373.  
  374. #endif /* PRIMOS_GETENV */
  375.  
  376. #ifdef EBCDIC
  377. static int cnva[]={
  378.   0,  1,  2,  3, 55, 45, 46, 47, 22,  5, 37, 11, 12, 13, 14, 15,
  379.  16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
  380.  64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
  381. 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
  382. 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
  383. 215,216,217,226,227,228,229,230,231,232,233,173,224,189,113,109,
  384. 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
  385. 151,152,153,162,163,164,165,166,167,168,169,192, 79,208, 95,  7,
  386.  32, 33, 34, 35, 36, 21,  6, 23, 40, 41, 42, 43, 44,  9, 10, 27,
  387.  48, 49, 26, 51, 52, 53, 54,  8, 56, 57, 58, 59,  4, 20, 62,225,
  388.  65, 66, 74,114, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
  389.  88, 89, 98, 99,100,101,102,103,104,105,112, 67, 68,115,116,117,
  390. 118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
  391. 159,160,170,171,172,161,174,175,176,177,178,179,180,181,182,183,
  392. 184,185,186,187,188,106,190,191,202,203,204,205,206,207,218,219,
  393. 220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
  394.   };
  395.  
  396. char atoe(char c)         /* convert the c from ascii to ebcdic */
  397. {
  398.  return(cnva[c]);
  399. }
  400. #endif /* EBCDIC */
  401.  
  402. /*
  403. Local Variables:
  404. c-indent-level: 4
  405. c-continued-statement-offset: 4
  406. c-brace-offset: -4
  407. c-argdecl-indent: 0
  408. c-label-offset: -4
  409. tab-width: 4
  410. End:
  411. */
  412.  
  413.